'use client'

import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card'
import { Badge } from '@/components/ui/badge'
import { CalendarIcon, ClockIcon, CheckCircleIcon, XCircleIcon, TrendingUpIcon, UserIcon } from 'lucide-react'

interface AppointmentStatsProps {
  stats: {
    total: number
    pending: number
    confirmed: number
    completed: number
    cancelled: number
    todayAppointments: number
    upcomingAppointments: number
    thisWeekGrowth?: number
    averageWaitTime?: number
  }
  className?: string
}

export default function AppointmentStats({ stats, className = '' }: AppointmentStatsProps) {
  const completionRate = stats.total > 0 ? Math.round((stats.completed / stats.total) * 100) : 0
  const confirmationRate = stats.total > 0 ? Math.round(((stats.confirmed + stats.completed) / stats.total) * 100) : 0

  return (
    <div className={`grid gap-4 md:grid-cols-2 lg:grid-cols-4 ${className}`}>
      {/* 总预约数 */}
      <Card>
        <CardHeader className="flex flex-row items-center justify-between space-y-0 pb-2">
          <CardTitle className="text-sm font-medium">总预约数</CardTitle>
          <CalendarIcon className="h-4 w-4 text-muted-foreground" />
        </CardHeader>
        <CardContent>
          <div className="text-2xl font-bold">{stats.total}</div>
          <p className="text-xs text-muted-foreground">
            今日新增 {stats.todayAppointments} 个
            {stats.thisWeekGrowth !== undefined && (
              <span className={`ml-2 inline-flex items-center text-xs ${
                stats.thisWeekGrowth >= 0 ? 'text-green-600' : 'text-red-600'
              }`}>
                <TrendingUpIcon className={`h-3 w-3 mr-1 ${
                  stats.thisWeekGrowth < 0 ? 'rotate-180' : ''
                }`} />
                {Math.abs(stats.thisWeekGrowth)}%
              </span>
            )}
          </p>
        </CardContent>
      </Card>

      {/* 待确认 */}
      <Card>
        <CardHeader className="flex flex-row items-center justify-between space-y-0 pb-2">
          <CardTitle className="text-sm font-medium">待确认</CardTitle>
          <ClockIcon className="h-4 w-4 text-yellow-600" />
        </CardHeader>
        <CardContent>
          <div className="text-2xl font-bold text-yellow-600">{stats.pending}</div>
          <div className="flex items-center justify-between">
            <p className="text-xs text-muted-foreground">需要及时处理</p>
            {stats.pending > 0 && (
              <Badge variant="outline" className="text-yellow-600 border-yellow-600">
                紧急
              </Badge>
            )}
          </div>
          {stats.averageWaitTime && (
            <p className="text-xs text-muted-foreground mt-1">
              平均等待 {stats.averageWaitTime} 分钟
            </p>
          )}
        </CardContent>
      </Card>

      {/* 已确认 */}
      <Card>
        <CardHeader className="flex flex-row items-center justify-between space-y-0 pb-2">
          <CardTitle className="text-sm font-medium">已确认</CardTitle>
          <CheckCircleIcon className="h-4 w-4 text-blue-600" />
        </CardHeader>
        <CardContent>
          <div className="text-2xl font-bold text-blue-600">{stats.confirmed}</div>
          <p className="text-xs text-muted-foreground">
            即将就诊 {stats.upcomingAppointments} 个
          </p>
          <div className="mt-1">
            <div className="flex items-center justify-between text-xs">
              <span className="text-muted-foreground">确认率</span>
              <span className="font-medium text-blue-600">{confirmationRate}%</span>
            </div>
          </div>
        </CardContent>
      </Card>

      {/* 已完成 */}
      <Card>
        <CardHeader className="flex flex-row items-center justify-between space-y-0 pb-2">
          <CardTitle className="text-sm font-medium">已完成</CardTitle>
          <CheckCircleIcon className="h-4 w-4 text-green-600" />
        </CardHeader>
        <CardContent>
          <div className="text-2xl font-bold text-green-600">{stats.completed}</div>
          <p className="text-xs text-muted-foreground">
            完成率 {completionRate}%
          </p>
          <div className="mt-1">
            <div className="w-full bg-gray-200 rounded-full h-2">
              <div 
                className="bg-green-600 h-2 rounded-full transition-all duration-300" 
                style={{ width: `${completionRate}%` }}
              />
            </div>
          </div>
        </CardContent>
      </Card>

      {/* 状态分布卡片 */}
      <Card className="md:col-span-2 lg:col-span-4">
        <CardHeader>
          <CardTitle className="text-sm font-medium">预约状态分布</CardTitle>
        </CardHeader>
        <CardContent>
          <div className="grid gap-4 md:grid-cols-4">
            <div className="flex items-center space-x-3">
              <div className="w-3 h-3 bg-yellow-500 rounded-full"></div>
              <div className="flex-1">
                <div className="flex items-center justify-between">
                  <span className="text-sm font-medium">待确认</span>
                  <span className="text-sm text-muted-foreground">{stats.pending}</span>
                </div>
                <div className="w-full bg-gray-200 rounded-full h-2 mt-1">
                  <div 
                    className="bg-yellow-500 h-2 rounded-full" 
                    style={{ width: `${stats.total > 0 ? (stats.pending / stats.total) * 100 : 0}%` }}
                  />
                </div>
              </div>
            </div>

            <div className="flex items-center space-x-3">
              <div className="w-3 h-3 bg-blue-500 rounded-full"></div>
              <div className="flex-1">
                <div className="flex items-center justify-between">
                  <span className="text-sm font-medium">已确认</span>
                  <span className="text-sm text-muted-foreground">{stats.confirmed}</span>
                </div>
                <div className="w-full bg-gray-200 rounded-full h-2 mt-1">
                  <div 
                    className="bg-blue-500 h-2 rounded-full" 
                    style={{ width: `${stats.total > 0 ? (stats.confirmed / stats.total) * 100 : 0}%` }}
                  />
                </div>
              </div>
            </div>

            <div className="flex items-center space-x-3">
              <div className="w-3 h-3 bg-green-500 rounded-full"></div>
              <div className="flex-1">
                <div className="flex items-center justify-between">
                  <span className="text-sm font-medium">已完成</span>
                  <span className="text-sm text-muted-foreground">{stats.completed}</span>
                </div>
                <div className="w-full bg-gray-200 rounded-full h-2 mt-1">
                  <div 
                    className="bg-green-500 h-2 rounded-full" 
                    style={{ width: `${stats.total > 0 ? (stats.completed / stats.total) * 100 : 0}%` }}
                  />
                </div>
              </div>
            </div>

            <div className="flex items-center space-x-3">
              <div className="w-3 h-3 bg-red-500 rounded-full"></div>
              <div className="flex-1">
                <div className="flex items-center justify-between">
                  <span className="text-sm font-medium">已取消</span>
                  <span className="text-sm text-muted-foreground">{stats.cancelled}</span>
                </div>
                <div className="w-full bg-gray-200 rounded-full h-2 mt-1">
                  <div 
                    className="bg-red-500 h-2 rounded-full" 
                    style={{ width: `${stats.total > 0 ? (stats.cancelled / stats.total) * 100 : 0}%` }}
                  />
                </div>
              </div>
            </div>
          </div>

          <div className="mt-6 pt-4 border-t">
            <div className="grid gap-4 md:grid-cols-3">
              <div className="text-center">
                <div className="text-2xl font-bold text-blue-600">{stats.upcomingAppointments}</div>
                <div className="text-sm text-muted-foreground">即将就诊</div>
              </div>
              <div className="text-center">
                <div className="text-2xl font-bold text-green-600">{confirmationRate}%</div>
                <div className="text-sm text-muted-foreground">确认率</div>
              </div>
              <div className="text-center">
                <div className="text-2xl font-bold text-orange-600">{stats.todayAppointments}</div>
                <div className="text-sm text-muted-foreground">今日预约</div>
              </div>
            </div>
          </div>
        </CardContent>
      </Card>
    </div>
  )
}